package www.golf.web.gonwubu;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import www.golf.ibatis.IBatisFactory;
import www.golf.ibatis.bean.Project;
import www.golf.web.session.WebSession;
import www.golf.web.util.UtilListbox;
import www.golf.web.util.UtilZKObject;

public class ProjectControl extends WebSession{
	UtilZKObject ZKObj =new UtilZKObject(this);
	UtilListbox lb= new UtilListbox();
	public void initialize() {
		List list=null;
		try {
			list = (List)dbSession().queryForList("Project.selectPro");
			dbSessionClose();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		lb.setComponentData(this, "grid", list, new ListitemRenderer(){
			public void render(Listitem item, Object data) throws Exception {
				Project form = (Project) data;
				Listcell name = new Listcell();
				name.setLabel(form.getName());
				name.setParent(item);		
				
				Listcell info = new Listcell();
				info.setLabel(form.getInfo());
				info.setParent(item);		
				
				Listcell pkey = new Listcell();
				pkey.setLabel(String.valueOf(form.getPkey()));
				pkey.setParent(item);
				pkey.setVisible(false);
			}		
		});
		if(getUserPower()<=15){
			getFellow("add").setVisible(true);
			getFellow("edit").setVisible(true);
			//getFellow("delete")
		}
	}
	public void setEditdata(UtilZKObject zobj){
		zobj.setObjectValue("ed_name", lb.getListcellValue(this, "grid", 0));
		zobj.setObjectValue("ed_info", lb.getListcellValue(this, "grid", 1));
	}
	public void onEdit(){
		
		String value=lb.getListcellValue(this, "grid", 2);
		if(value!=null){
			try {
				final int key =Integer.parseInt(value);
				final Window win=(Window)Executions.createComponents(getPagefileSession()+"edit.zul", null, null);
				final UtilZKObject zobj= new UtilZKObject(win);
				Events.postEvent("onModal", win, null);
				setEditdata(zobj);
				win.getFellow("ed_submit").addEventListener("onClick", new EventListener(){
					public void onEvent(Event event) throws Exception {
						Project pj= new Project();
						pj.setPkey(key);
						pj.setName(zobj.getValue("ed_name"));
						pj.setInfo(zobj.getValue("ed_info"));
						dbSession().update("Project.updatePro", pj);
						Messagebox.show("修改完毕");
						win.detach();
						initialize();
					}
				});
				win.getFellow("ed_cancel").addEventListener("onClick",new EventListener(){
					public void onEvent(Event event) throws Exception {
						win.detach();
					}
				});
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
	}
	public void onDelete(){
		String value=lb.getListcellValue(this, "grid", 2);
		if(value!=null){
			int key =Integer.parseInt(value);
			try {
				if(Messagebox.show("删除可能造成系统混乱", "注意", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION )==Messagebox.YES){
					dbSession().delete("Project.deletePro",key);
					initialize();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public void onSubmit() {
		HashMap<String,String> map=new HashMap<String, String>();
		map.put("name", ZKObj.getValue("pr_name"));
		map.put("info", ZKObj.getValue("pr_info"));
		try {
			dbSession().insert("Project.insertPro",map);
			dbSessionClose();
		} catch (Exception e) {
			e.printStackTrace();
		}
		initialize();
		((Textbox)getFellow("pr_name")).setConstraint("");
		((Textbox)getFellow("pr_name")).setText("");
		((Textbox)getFellow("pr_name")).setConstraint("no empty");
	}
	
}
